package com.vs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vs.common.DuplicateTypeException;
import com.vs.common.SessionKeys;
import com.vs.dao.ExpensesTypeOperation;
import com.vs.model.CustomMessage;
import com.vs.model.ExpenseTypeForm;
import com.vs.model.User;
import com.vs.util.Constants;

public class ExpenseTypeAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String next = "";
		ExpenseTypeForm expenseTypeForm = (ExpenseTypeForm) form;
		Constants.log.debug(expenseTypeForm);
		
		ArrayList<CustomMessage> customErrors = new ArrayList<CustomMessage>();
		List<ExpenseTypeForm> expenseTypeList = null;
		
		HttpSession session = getActiveSession(request);
		User user = (User) session.getAttribute(SessionKeys.CURRENT_USER);
		expenseTypeForm.setAddedBy(user.getUserName());
		
		String action = request.getParameter("action");
		Constants.log.debug("action1=" + action);
		System.out.println("action1=" + action);

		if (action == null) {
			action = expenseTypeForm.getAction();
			System.out.println("action2=" + action);
		}
		
		if (action != null) {
			if (action.equals("selectAll")) {
				next = "success";
			} else if (action.equals("add")) {
				try{
				boolean result = new ExpensesTypeOperation()
						.addExpenseType(expenseTypeForm);
				if (result) {
					next = "success";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("expenseType "
							+ expenseTypeForm.getExpenseType()
							+ " is added successfully");
					customErrors.add(info);
				} else {
					next = "addError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("error while adding expense type");
					customErrors.add(info);
				}
				}
				catch (DuplicateTypeException e) {
					next = "addError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("expenseType "+expenseTypeForm.getExpenseType() +" , already present in the system");
					customErrors.add(info);
				}

			} else if (action.equals("delete")) {

				Integer expenseTypeId = Integer.parseInt(request
						.getParameter("expenseTypeId"));
				boolean result = new ExpensesTypeOperation()
						.deleteExpenseType(expenseTypeId);
				if (result) {
					next = "success";
				CustomMessage info = new CustomMessage();
				info.setMessageLevel(Constants.INFO);
				info.setMessageText("expenseTypeId " + expenseTypeId
						+ " is deleted successfully");
				customErrors.add(info);
				}
				else
				{
					next = "success";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("error while deleting expense type");
					customErrors.add(info);
				}
			}
		}

		if (next.equals("success") || next.equals("addError")) {
			expenseTypeList = new ExpensesTypeOperation().getAllExpenseTypes();
			updateExpenseTypesInSession(request);
			
			if(expenseTypeList==null || expenseTypeList.size()==0)
			{
				CustomMessage info = new CustomMessage();
				info.setMessageLevel(Constants.INFO);
				info.setMessageText("At present, no expense types are created in the system");
				customErrors.add(info);
			}
		}
		
		request.setAttribute("customErrors", customErrors);
		request.setAttribute("expenseTypeList", expenseTypeList);
		System.out.println("added expenseTypeList to request object");

		return mapping.findForward(next);
	}

	private HttpSession getActiveSession(HttpServletRequest request) {
		return request.getSession(false);
	}

	private void updateExpenseTypesInSession(HttpServletRequest request) {
		HttpSession session = getActiveSession(request);
		session.removeAttribute("expenseTypes");
		session.setAttribute("expenseTypes",
				new ExpensesTypeOperation().getExpenseTypeLOV());
	}
}
